<?php
/**
 * 其他出库
 * Created by PhpStorm.
 * User: echa
 * Date: 2018/3/22
 * Time: 11:07
 */
namespace App\Services\Warehouse;
class MisDeliveryService extends PublicService{

    public function __construct()
    {
        parent::__construct(config('k3cloud.MisDelivery'));
    }


    /***
     * 1.formid：业务对象表单Id（必录）
    2.data：Json格式数据（详情参考Json格式数据）（必录）
    2.1.Creator：创建者名字（非必录）
    2.2.NeedUpDateFields：需要保存的字段,格式["fieldkey1","fieldkey2","entitykey1",...]，数组类型(非必录)
    2.3.NeedReturnFields：需要返回的结果字段,格式["fieldkey","entitykey.fieldkey",...]（非必录）
    2.4.IsDeleteEntry：是否删除分录，默认True删除（非必录）
    2.5.SubSystemId：菜单所在子系统Id（非必录）
    2.6.IsVerifyBaseDataField：是否验证所有的基础资料，默认False不验证（非必录）
    2.7.IsEntryBatchFill：是否批量填充分录：默认为Ture（非必录）
    2.8.Model：表单实体数据（必录）
     * @param array $arrdata
     * @param array $is_submitandaudit  是否创建并提交审核  默认否
    @author echa
     * @return bool
     */
    public function SaveMisDelivery(array $arrdata,$is_submitandaudit=false){
        $goods=$this->PostGoodsData($arrdata['GoodsData']);
        if(!$goods){
            return false;
        }
        if(empty($arrdata['FID'])){
            $arrdata['FID']=0;
        }
        if(empty($arrdata['FBillNo'])){
            $arrdata['FBillNo']="";
        }
        $FOwnerTypeBDSupplier=config('k3cloud.FOwnerTypeBDSupplier');
        $FOwnerTypeBDOwnerOrg=config('k3cloud.FOwnerTypeBDOwnerOrg');
        $FOwnerFNumber=config('k3cloud.organization');
        if($arrdata['k3_vmi']){
            $FOwnerTypeID=$FOwnerTypeBDSupplier;
            $FOwnerFNumber=$arrdata['k3_supplier_sn'];
        }else{
            $FOwnerTypeID=$FOwnerTypeBDOwnerOrg;
        }
        if(empty($arrdata['note'])){
            $arrdata['note']="";
        }
        $data=[
            "FID"=>$arrdata['FID'],
            "FBillNo"=>$arrdata['FBillNo'],
            "FBillTypeID"=>[
                "FNumber"=>"QTCKD01_SYS"
            ],
            "FStockOrgId"=>[
                "FNumber"=>config('k3cloud.organization'),
            ],
            "FPickOrgId"=>[
                "FNumber"=>config('k3cloud.organization'),
            ],
            "FStockDirect"=>"GENERAL",
            "FDate"=>date('Y-m-d H:i:s',time()),
            "FCustId"=>[
                "FNumber"=>config('k3cloud.appcustomer'),
            ],
            "FDeptId"=>[
                "FNumber"=>""
            ],
            "FStockerId"=>[
                "FName"=>""
            ],
            "FStockerGroupId"=>[
                "FNumber"=>""
            ],
            "FBizType"=>"0",
            "FOwnerTypeIdHead"=>$FOwnerTypeID,
            "FOwnerIdHead"=>[
                "FNumber"=>$FOwnerFNumber,
            ],
            "FNote"=>$arrdata['note'],
            "FBaseCurrId"=>[
                "FNumber"=>"PRE001"
            ],
            "FScanBox"=>"",
            "F_PAEZ_Combo"=>$arrdata['MD_type'],  //出库类型
            "FEntity"=>$goods
        ];
        if($is_submitandaudit){
            $return=$this->save($data);
            if(empty($return['Id'])){
                return false;
            }
            $submitreturn=$this->SubmitMisDelivery([$return['Number']]);
            if(empty($submitreturn['Id'])){
                return false;
            }
            $auditreturn=$this->AuditMisDelivery([$return['Number']]);
            if(empty($auditreturn['Id'])){
                return false;
            }
            return $auditreturn;
        }else{
            return $this->save($data);
        }
    }

    /**
    提交
     *
     * 1.formid：业务对象表单Id（必录）
    2.data：Json格式数据（详情参考Json格式数据）（必录）
    2.1.CreateOrgId：创建者组织，字符串类型（非必录）
    2.2.Numbers：单据编号集合，格式["billNo1","billNo1",...]，数组类型（必录）
     */

    public function  SubmitMisDelivery($Numbers=[]){
        return $this->submit($Numbers);
    }

    /**
    审核
     * 1.formid：业务对象表单Id（必录）
    2.data：Json格式数据（详情参考Json格式数据）（必录）
    2.1.CreateOrgId：创建者组织，字符串类型（非必录）
    2.2.Numbers：单据编号集合，格式["billNo1","billNo1",...]，数组类型（必录）
     */
    public function AuditMisDelivery($Numbers=[]){
        return $this->audit($Numbers);
    }

    /**
    反审核
     * 1.formid：业务对象表单Id（必录）
    2.data：Json格式数据（详情参考Json格式数据）（必录）
    2.1.CreateOrgId：创建者组织，字符串类型（非必录）
    2.2.Numbers：单据编号集合，格式["billNo1","billNo1",...]，数组类型（必录）
     */
    public function unAuditMisDelivery($Numbers=[]){
        return $this->unAudit($Numbers);
    }

    /**
     *单据列表查询
     * 1.data：Json格式数据（详情参考Json格式数据）（必录）
    1.1.FormId：业务对象表单Id（必录）
    1.2.FieldKeys：字段keys，字符串类型用逗号分隔，比如"key1,key2..."（必录）
    1.3.FilterString：过滤（非必录）  FNumber=''
    1.4.OrderString：排序字段（非必录）
    1.5.TopRowCount：总行数（非必录）
    1.6.StartRow：开始行（非必录）
    1.7.Limit：最大行数，不能超过2000（非必录）
     */
    public function ExecuteBillQueryMisDelivery($FieldKeys='',$FilterString='',$OrderString='',$TopRowCount=0,$StartRow=0,$Limit=20)
    {
        if (empty($FieldKeys)) {
            $FieldKeys='FBillNo,F_PAEZ_Combo,FDocumentStatus,FCreateDate,FCustId,FCreatorId,FCreateDate';
        }
        return $this->ExecuteBillQuery($FieldKeys,$FilterString,$OrderString,$TopRowCount,$StartRow,$Limit);

    }

    public function PostGoodsData(array $goodsdata){
        if(!is_array($goodsdata)){
            return false;
        }
        $goods=array();
        $FOwnerTypeBDSupplier=config('k3cloud.FOwnerTypeBDSupplier');
        $FOwnerTypeBDOwnerOrg=config('k3cloud.FOwnerTypeBDOwnerOrg');
        foreach ($goodsdata as $k=>$v){
            $FOwnerFNumber=config('k3cloud.organization');
            if($v['k3_vmi']){
                $FOwnerTypeID=$FOwnerTypeBDSupplier;
                $FOwnerFNumber=$v['k3_supplier_sn'];
            }else{
                $FOwnerTypeID=$FOwnerTypeBDOwnerOrg;
            }
            $goods[]=[
                "FEntryID"=>null,
                "FMaterialId"=>[
                    "FNumber"=>$v['goods_sn']
                ],
                "FUnitID"=>[
                    "FNumber"=>$v['spec_unit']
                ],
                "FQty"=>$v['goods_number'],
                "FBaseUnitId"=>[
                    "FNumber"=>"Pcs"
                ],
                "FStockId"=>[
                    "FNumber"=>$v['StockNubmer']
                ],
                "FStockLocId"=>[
                    "FSTOCKLOCID__FF100001"=>[
                        "FNUMBER"=>""
                    ]
                ],
                "FLot"=>[
                    "FNumber"=>""
                ],
                "FPRODUCTGROUPID"=>[
                    "FNumber"=>""
                ],
                "FOwnerTypeId"=>$FOwnerTypeID,
                "FOwnerId"=>[
                    "FNumber"=>$FOwnerFNumber
                ],
                "FEntryNote"=>null,
                "FBomId"=>[
                    "FNumber"=>""
                ],
                "FProjectNo"=>null,
                "FProduceDate"=>null,
                "FServiceContext"=>null,
                "FStockStatusId"=>[
                    "FNumber"=>"KCZT01_SYS"
                ],
                "FMtoNo"=>null,
                "FCostItem"=>[
                    "FNumber"=>""
                ],
                "FKeeperTypeId"=>"BD_KeeperOrg",
                "FDistribution"=>false,
                "FKeeperId"=>[
                    "FNumber"=>config('k3cloud.organization')
                ],
                "FExtAuxUnitId"=>[
                    "FNumber"=>""
                ],
                "FExtAuxUnitQty"=>0.0
            ];
        }
        return $goods;
    }
}